<?php
/*
*	$version: 2008-1-4 15:18:41 beta 1.0
*	$id: validate.php 
*	$author: taoFeng
*	$mailto: font99@Gmail.com
*	$license: This is not a freeware
*/

class Validate
{
	/*
	*	boolean _isSBCCase (String $data);
	*/
	public static function _isSBCCase($_d){
		$n = 0;
		while ($n < strlen($_d)){
			if (ord($_d{$n++}) <= 127) return false;
		}
		return true;
	}

	/*
	*	boolean _isChineseUtf8 (String $data);
	*/
	public static function _isChineseUtf8($_d){
		if (preg_match("/^([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}/",$word) || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}$/",$word) || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){2,}/",$word)) {return true;} else {return false;}
	}

	/*
	*	boolean _maxLength (String $data,[Integer $len]);
	*/
	public static function _maxLength($_d , $_l = 4){
		if (mb_strlen($_d) > $_l) return false; else return true;
	}

	/*
	*	boolean _isSafeData (String $data);
	*/
	public static function _isSafeData($_d){
		return (Bool)preg_match("/[\\\\\/\:?*\"<>|;%\'-]+/i", $_d);
	}

	/*
	*	boolean _isEmail (String $data);
	*/
	public static function _isEmail($_d){
		return (Bool)preg_match("/^([\\w\\d\\-\\.]+)@{1}(([\\w\\d\\-]{1,67})|([\\w\\d\\-]+\\.[\\w\\d\\-]{1,67}))\\.(([a-zA-Z\\d]{2,4})(\\.[a-zA-Z\\d]{2})?)$/i",$_d);
	}

	/*
	*	boolean _isValidURL (String $data,$emailto = true);
	*/
	public static function _isValidURL($_d,$_emailto = true){
		if (preg_match("/^
			(?:https?|ftp):\\/\\/
			(?:
				(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+
				[a-zA-Z0-9]
				(?:[a-zA-Z0-9-]*[a-zA-Z0-9])?
			|
				\\[
				(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}
				(?:
					25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-zA-Z0-9-]*[a-zA-Z0-9]:
					(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21-\\x5A\\x53-\\x7F]
						|\\\\[\\x01-\\x09\\x0B\\x0C\\x0E-\\x7F])+
				)
				\\]
			)
			(?::[0-9]{1,5})?
			(?:[\\/\\?\\#][^\\n\\r]*)?
			$/Dx", $_d)) return true;

		if (preg_match("/^[^:]+([\\/\\\\?#][^\\r\\n]*)?$/D", $_d))
			return true;

		if ($_emailto)
			if (substr($_d, 0, 7) == "mailto:")
				return Validate::_isEmail(substr($_d, 7));

		return false;		
	}
}


?>